package com.example.View.Renderers;

import java.util.HashMap;
import java.util.Map;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;

import com.example.R;
import com.example.View.Projection;
import com.example.models.Tower;
import com.example.util.Figure;

public class TowerRenderer extends FigureRendererBase {

    // The paint to be used for rendering the pac-man in normal mode (yellow)
    private Paint normalPaint;

    private Map<String, Bitmap> iconByName;

    /**
     * Constructs a PacManRenderer
     */
    public TowerRenderer(Resources resources) {
        super(resources);
        normalPaint = new Paint();
        normalPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        normalPaint.setARGB(255, 255, 255, 0);
        normalPaint.setAntiAlias(true);
        iconByName = new HashMap<String, Bitmap>();
        iconByName.put("ArrowTower", createBitmap(R.drawable.arrow_tower));
        iconByName.put("Catapult", createBitmap(R.drawable.catapult_tower));
        iconByName.put("FlameThrower", createBitmap(R.drawable.flamethrower_tower));
        iconByName.put("TeslaCoil", createBitmap(R.drawable.teslacoil_tower));
        iconByName.put("DeafultTowerName", createBitmap(R.drawable.arrow_tower));
    }

    /**
     * Renders a given figure
     * @param canvas The canvas on which (and using which) this figure is rendered
     * @param projection The translation between the model coordinate system and the view coordinate system.
     * @param figure The figure to be rendered.
     */
    @Override
    public void render(Canvas canvas, Projection projection, Figure figure) {
        Tower tower = (Tower)figure;
        Bitmap bitmap = getBitmap(tower);
        canvas.drawBitmap(bitmap, figure.getX() * projection.getPixelsPerCell() - projection.getX(), figure.getY() * projection.getPixelsPerCell() - projection.getY() - bitmap.getHeight() + projection.getPixelsPerCell(), normalPaint);
    }

    /**
     * Returns the bitmap to be used for a given ghost
     */
    public Bitmap getBitmap(Tower tower) {
        String name = tower.getName();
        Bitmap bitmap = iconByName.get(name);
        //TODO:
        //if (tower.getPlayerId() == 2) {
        //	bitmap = Bitmap.createScaledBitmap(bitmap, -bitmap.getWidth(), bitmap.getHeight(), true);
        //}
        return (bitmap);
    }

}
